package com.study.www.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.study.www.config.Result;
import com.study.www.entity.UserVo;
import com.study.www.enums.ValidateParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author : 89007337.zhuyanpeng
 * @Description: :
 * @date :Create in  2019/6/27- 21:17
 * @Version: V1.0
 * @Modified By:
 **/
@RestController()
@RequestMapping("/test")
@Slf4j
@Validated
public class UserController {

    static Map<String,UserVo> userVoMap = new HashMap<>();

    @GetMapping(value = {"","/{id}"})
    public Result get(@PathVariable(value = "id",required = false)
                          @Pattern(regexp = ValidateParams.validateStr, message = "id不正确")  String id,
                      @Validated(value = UserVo.Search.class)  UserVo query){
        if(StringUtils.isNotBlank(id)){
            UserVo userVo = userVoMap.get(id);
            return Result.success(userVo);
        }
        System.out.println(query);
        String name = query.getName();
        if (StringUtils.isNotBlank(name)) {
            AtomicReference<Result<UserVo>> success = null;
            userVoMap.values().stream().forEach(userVo -> {
                if (name.equals(userVo.getName())){
                    success.set(Result.success(userVo));
                }
            });
            return success.get();
        }
        Result<Collection<UserVo>> success = Result.success(userVoMap.values());
        System.out.println(JSON.toJSONString(success));
        return success;
    }

    @PutMapping("/get/{id}")
    public Result put( @Validated @PathVariable("id")     @Pattern(regexp = ValidateParams.validateStr, message = "id不正确")  String id,
                       @RequestParam(value = "name",required = false) @Pattern(regexp =  ValidateParams.validateNameStr, message = "name不正确")  String name){
        log.warn("id"+id);
        log.warn("name"+name);
        return Result.success();
    }

    @PostMapping("/post")
    public Result post(@Validated UserVo query){
        System.out.println(query);
        return Result.success();
    }

    @PostMapping(value = {"/",""})
    public Result post(@RequestBody UserVo userVo, ModelMap modelMap){
       save(userVo);
        return Result.success();
    }

    public void save(UserVo userVo) {
        if (StringUtils.isBlank(userVo.getId())) {
            String id = RandomStringUtils.randomAlphanumeric(10);
            userVo.setId(id);
        }
        userVoMap.put(userVo.getId(),userVo);
    }

    @DeleteMapping("/del")
    public Result del(@Valid UserVo query){
        System.out.println(query);
        return Result.success();
    }

    @PutMapping("/put")
    public Result put(@Valid UserVo query){
        System.out.println(query);
        return Result.success();
    }
}
